package fimuni.pb138.repositar;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

public class ArtifactUtils {
	public static final String DATABASE_NAME = "artifacts";
	public static final String DATABASE_SERVER = "localhost";
	public static final int DATABASE_PORT = 1984;
	public static final String DATABASE_USERNAME = "admin";
	public static final String DATABASE_PASSWORD = "admin";

	//public static final String STORAGE_PATH = "D:\\Program Files\\Spring Source\\vfabric-tc-server-developer-2.8.2.RELEASE\\base-instance\\wtpwebapps\\repositar\\WEB-INF\\classes\\files\\";
	public static final String STORAGE_PATH = "D:\\dev\\java\\repositar\\";
	public static final String STORAGE_DIR = "\\..\\";
	public static final String STORAGE_PREFIX = "art";

	public enum ArtifactType {
		WAR, XSD, WSDL;
	}

	public static String saveFile(MultipartFile file) {
		File newFile;
		try {
			File a = new File(STORAGE_PATH);
			newFile = File.createTempFile(STORAGE_PREFIX, "", a);
			
			FileUtils.writeByteArrayToFile(newFile, file.getBytes());
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return newFile.getName();
	}

	public static final String XQUERY_WAR_FILTER = "declare namespace ns = \"http://java.sun.com/xml/ns/javaee\";"
			+ "let $doc := doc(\"DOCNAME\") "
			+ "for $filters in $doc//ns:filter "
			+ "let $name := $filters/ns:filter-name "
			+ "return concat(string($name),\"|\")";

	public static final String XQUERY_WAR_LISTENER = "declare namespace ns = \"http://java.sun.com/xml/ns/javaee\";"
			+ "let $doc := doc(\"DOCNAME\")"
			+ "for $listeners in $doc//ns:listener "
			+ "let $name := $listeners/ns:listener-class "
			+ "return concat(string($name),\"|\")";
	
	public static final String XQUERY_WSDL_OPERATIONS = "declare namespace ns = \"http://schemas.xmlsoap.org/wsdl/\";"
			+ "let $doc := doc(\"DOCNAME\")"			
			+ "for $operations in $doc//ns:operation "
			+ "let $name := $operations/@name "
			+ "let $request := $operations/ns:input/@message "
			+ "let $response := $operations/ns:output/@message "
			+ "where not(empty($name)) "
			+ "return concat(string($name),\"#\",string($request),\"#\",string($response),\"|\")";
	
	public static final String XQUERY_XSD_ATTRIBUTES = "declare namespace ns = \"http://www.w3.org/2001/XMLSchema\";"
			+ "let $doc := doc(\"DOCNAME\") "			
			+ "for $attributes in $doc//ns:attribute "
			+ "let $name := $attributes/@name "
			+ "return concat(string($name),\"|\")";
	
	public static final String XQUERY_XSD_COMPLEXTYPES = "declare namespace ns = \"http://www.w3.org/2001/XMLSchema\";"
			+ "let $doc := doc(\"DOCNAME\") "			
			+ "for $types in $doc//ns:complexType "
			+ "let $name := $types/@name "
			+ "where not(empty($name)) "
			+ "return concat(string($name),\"|\")";
	
	public static final String XQUERY_XSD_SIMPLETYPES = "declare namespace ns = \"http://www.w3.org/2001/XMLSchema\";"
			+ "let $doc := doc(\"DOCNAME\") "			
			+ "for $types in $doc//ns:simpleType "
			+ "let $name := $types/@name "
			+ "where not(empty($name)) "
			+ "return concat(string($name),\"|\")";
	
	public static final String XQUERY_XSD_ELEMENTS = "declare namespace ns = \"http://www.w3.org/2001/XMLSchema\";"
			+ "let $doc := doc(\"DOCNAME\") "			
			+ "for $elements in $doc//ns:element "
			+ "let $name := $elements/@name "
			+ "return concat(string($name),\"|\")";
}
